<?php
/**
 * php GEM Project
 * 
 * LICENSE
 * 
 * PHP System Architecture Enabling Rapid Application Deployment and Scalability
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @copyright Copyright (C) 2010 Zeletron LLC (http://zeletron.com)
 * @license http://gem.zeletron.com/license
 */

/**
 * GEM/Validate/PasswordStrength.php
 * 
 * This is a slightly modified version of the example custom validator
 * located in the Zend Framework Documentation (58.4).
 * The validator examines a supplied password string and determines its strength.
 * 
 * @author David Joly | Zend
 * @category GEM
 * @package GEM_Validate
 */

class GEM_Validate_PasswordStrength extends Zend_Validate_Abstract
{
	/**
	 * Validation failure message keys.
	 */
	const PASSWORD_NO_SPECIAL_CHAR 	= 'noSpecialChar';
	const PASSWORD_TOO_SHORT 		= 'passwordTooShort';
	const PASSWORD_TOO_LONG 		= 'passwordTooLong';
	const PASSWORD_NO_LOWERCASE 	= 'passwordNoLowerCase';
	const PASSWORD_NO_UPPERCASE 	= 'passwordNoUpperCase';
	const PASSWORD_NO_DIGIT 		= 'passwordNoDigit';
	
	/**
     * Validation failure message template definition
     *
     * @var array
     */
    protected $_messageTemplates = array(
     	self::PASSWORD_NO_SPECIAL_CHAR 	=> "Password should contain a special character.",
        self::PASSWORD_TOO_SHORT 		=> "Password too short! It should be 8 characters long.",
        self::PASSWORD_TOO_LONG 		=> "Password too long! Max is 32 characters.", 
        self::PASSWORD_NO_LOWERCASE 	=> "Your password should contain at least one lowercase letter.",
        self::PASSWORD_NO_UPPERCASE 	=> "Your password should contain at least one uppercase letter.",
        self::PASSWORD_NO_DIGIT 		=> "Your password should contain at least one digit."
    );
    
    public function __construct()
    {}
    
    /**
     * This function will return false if a user's password is not strong enough. 
     * Passwords must contain at least one lowercase, uppercase, and digit characters.
     * 
     * @see Zend_Validate_Interface#isValid()
     * @param $value 
     * @return bool
     */
    public function isValid($value)
    {
    	$this->_setValue($value);
    	
    	$isValid = true;

        if (strlen($value) < 8) {
            $this->_error(self::PASSWORD_TOO_SHORT);
            $isValid = false;
        }
		
        if(!preg_match('/\W/',$value)){
        	$this->_error(self::PASSWORD_NO_SPECIAL_CHAR);
            $isValid = false;
        }
        
    	if (strlen($value) > 32) {
            $this->_error(self::PASSWORD_TOO_LONG);
            $isValid = false;
        }
        
        if (!preg_match('/[A-Z]/', $value)) {
            $this->_error(self::PASSWORD_NO_UPPERCASE);
            $isValid = false;
        }

        if (!preg_match('/[a-z]/', $value)) {
            $this->_error(self::PASSWORD_NO_LOWERCASE);
            $isValid = false;
        }

        if (!preg_match('/\d/', $value)) {
            $this->_error(self::PASSWORD_NO_DIGIT);
            $isValid = false;
        }

        return $isValid;
    }
}